import Decimal from 'decimal.js'
import { MaterialModel } from './MaterialModel'

export class OrderItemModel {
  material?: MaterialModel

  quantity: number
  unitPrice: number
  amount: number

  constructor() {
    this.quantity = 0
    this.unitPrice = 0
    this.amount = 0
    this.material = undefined
  }

  public autoAmount() {
    const a = new Decimal(this.quantity)
    const b = new Decimal(this.unitPrice)
    this.amount = a.mul(b).floor().toNumber()
  }

  public static fromMaterialModel(material: MaterialModel): OrderItemModel {
    const oim = new OrderItemModel()
    oim.material = material
    oim.unitPrice = material.unitPrice
    return oim
  }
}
